package com.six.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.core.entity.CheckShop;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import com.six.shop.dao.CheckShopDao;
import com.six.shop.dao.ShopDao;
import com.six.shop.service.CheckShopService;
import com.six.shop.service.ShopService;
import com.six.shop.vo.SearchCheckShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName:CheckShopServiceImpl
 * Description:
 * date:2023/2/27 11:15
 *
 * @since JDK 1.8
 */
@Service
public class CheckShopServiceImpl extends ServiceImpl<CheckShopDao, CheckShop> implements CheckShopService {
    @Autowired
    private CheckShopDao checkShopDao;
    @Autowired
    private ShopDao shopDao;

    @Autowired
    private ShopService shopService;
    @Override
    public Result findAll(SearchCheckShop checkShop,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<CheckShop> list = checkShopDao.findAll(checkShop);
        PageInfo<CheckShop> pageInfo = new PageInfo<>(list);
        return new Result(2000,"查询成功",pageInfo);
    }

    @Override
    public Result updateState(CheckShop checkShop) {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.decodeJWT(token);
        Integer empid = (Integer) tokenChaim.get("empId");
        checkShop.setGmtModified(new Date());
        checkShop.setEmpid(empid.toString());
        int i = checkShopDao.updateById(checkShop);
        if (checkShop.getState()==1){
            shopDao.updateStatetrue(checkShop.getShopid());
        }
        return new Result(2000,"修改成功",i);
    }

    @Override
    public Result insertCheckShop(CheckShop checkShop) {
        int insert = checkShopDao.insert(checkShop);
        return new Result(2000,"添加成功",insert);
    }

    @Override
    public Result findMyCheck() {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) tokenChaim.get("tbUserid");
        Integer shopid = shopService.getShopId(tbUserid);
        CheckShop checkShop = checkShopDao.selectByShopid(shopid);
        System.out.println(checkShop);
        return new Result(2000,"查询成功",checkShop);
    }
}
